package com.xique.door.controller;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.request.elevator.ElevatorAddRequest;
import com.xique.door.bean.request.elevator.ElevatorAllListRequest;
import com.xique.door.bean.request.elevator.ElevatorEditRequest;
import com.xique.door.bean.request.elevator.ElevatorListRequest;
import com.xique.door.bean.vo.elevator.ElevatorSyncRequest;
import com.xique.door.biz.service.IElevatorBizService;
import com.xique.door.constant.ElevatorConstant;
import com.xique.door.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 电梯设备Controller
 *
 * @author caogq
 * @date 2021-10-08
 */
@RestController
@RequestMapping("/elevator")
public class ElevatorController extends BaseController {

    @Autowired
    private IElevatorBizService elevatorBizService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询电梯设备列表
     */
    @PreAuthorize(hasPermi = "door:elevator:list")
    @GetMapping("/list")
    public AjaxResult list(ElevatorListRequest request) {
        Object token = redisUtil.get(ElevatorConstant.IotSessionTokenRedisKey);
        if (null == token) {
            elevatorBizService.getSessionToken();
        }
        startPage();
        return elevatorBizService.elevatorList(request);
    }

    @PreAuthorize(hasPermi = "door:elevator:list")
    @GetMapping("/allList")
    public AjaxResult allList(@Valid ElevatorAllListRequest request) {
        return elevatorBizService.getAllElevatorList(request);
    }

    /**
     * 查询电梯详情
     */
    @PreAuthorize(hasPermi = "door:elevator:edit")
    @GetMapping("/detail")
    public AjaxResult detail(Long elvId) {
        return elevatorBizService.elevatorDetail(elvId);
    }

    /**
     * 新增保存电梯设备
     */
    @PreAuthorize(hasPermi = "door:elevator:add")
    @Log(title = "电梯设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@Valid @RequestBody ElevatorAddRequest request) {
        return elevatorBizService.addElevator(request);
    }

    @PreAuthorize(hasPermi = "door:elevator:add")
    @Log(title = "电梯设备", businessType = BusinessType.INSERT)
    @PostMapping("/addNotify")
    public AjaxResult addNotifySave(@RequestParam("url") String url) {
        return elevatorBizService.setNotify(url);
    }

    /**
     * 修改保存电梯设备
     */
    @PreAuthorize(hasPermi = "door:elevator:edit")
    @Log(title = "电梯设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@Valid @RequestBody ElevatorEditRequest request) {
        return elevatorBizService.editElevator(request);
    }

    /**
     * 删除电梯设备
     */
    @PreAuthorize(hasPermi = "door:elevator:remove")
    @Log(title = "电梯设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(String ids) {
        return elevatorBizService.removeElevator(ids);
    }

    /**
     * 获取session _token
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/getSessionToken")
    public AjaxResult getSessionToken() {
        return elevatorBizService.getSessionToken();
    }

    /**
     * 同步电梯基础信息
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncElvInfo")
    public AjaxResult syncElvInfo(@RequestBody ElevatorSyncRequest request) {
        return elevatorBizService.syncElvInfo(request.getElvId());
    }

    /**
     * 同步所有电梯基础信息
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncAllElvInfo")
    public AjaxResult syncAllElvInfo() {
        elevatorBizService.syncAllElvInfo(SecurityUtils.getParkId());
        return AjaxResult.success("同步所有电梯基础信息正在异步执行");
    }

    /**
     * 同步电梯运行统计
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncElvReport")
    public AjaxResult syncElvReport(Long elvId) {
        return elevatorBizService.syncElvReport(elvId);
    }

    /**
     * 同步所有电梯运行统计
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncAllElvReport")
    public AjaxResult syncAllElvReport(Long parkId) {
        elevatorBizService.syncAllElvReport(parkId);
        return AjaxResult.success("同步所有电梯运行统计正在异步执行");
    }

    /**
     * 同步电梯参数
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncElvParam")
    public AjaxResult syncElvParam(@RequestBody ElevatorSyncRequest request) {
        return elevatorBizService.syncElvParam(request.getElvId());
    }

    /**
     * 同步所有电梯参数
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncAllElvParam")
    public AjaxResult syncAllElvParam() {
        elevatorBizService.syncAllElvParam(SecurityUtils.getParkId());
        return AjaxResult.success("同步所有电梯参数正在异步执行");
    }

    /**
     * 同步运行集合
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncElvRuntime")
    public AjaxResult syncElvRuntime(Long elvId) {
        return elevatorBizService.syncRuntime(elvId);
    }

    /**
     * 同步故障集合
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncElvFault")
    public AjaxResult syncElvFault(Long elvId) {
        return elevatorBizService.syncFault(elvId);
    }

    /**
     * 同步事件集合
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncElvEvent")
    public AjaxResult syncElvEvent(Long elvId) {
        return elevatorBizService.syncEvent(elvId);
    }

    /**
     * 同步故障情况
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncElvFaultList")
    public AjaxResult syncElvFaultList(Long elvId) {
        return elevatorBizService.syncFaultList(elvId);
    }

    /**
     * 同步所有运行集合
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncAllElvRuntime")
    public AjaxResult syncAllElvRuntime(Long parkId) {
        elevatorBizService.syncAllRuntime(parkId);
        return AjaxResult.success("同步所有运行集合正在异步执行");
    }

    /**
     * 同步所有故障集合
     */
    @PostMapping("/syncAllElvFault")
    public AjaxResult syncAllElvFault(Long parkId) {
        elevatorBizService.syncAllFault(parkId);
        return AjaxResult.success("同步所有故障集合正在异步执行");
    }

    /**
     * 同步所有事件集合
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncAllElvEvent")
    public AjaxResult syncAllElvEvent(Long parkId) {
        elevatorBizService.syncAllEvent(parkId);
        return AjaxResult.success("同步所有事件集合正在异步执行");
    }

    /**
     * 同步所有故障情况
     */
    @PreAuthorize(hasPermi = "door:elevator:query")
    @PostMapping("/syncAllElvFaultList")
    public AjaxResult syncAllElvFaultList(Long parkId) {
        elevatorBizService.syncAllFaultList(parkId);
        return AjaxResult.success("同步所有故障情况正在异步执行");
    }


}
